package com.sgb.kjwl.view.ui.activity.self.mine;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.PermissionMenuEntity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.DialogHelperNew;
import com.swgk.core.util.MToast;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MangermoreModel  extends AndroidViewModel {

    public MangermoreModel(@NonNull Application application) {
        super(application);
    }

    public void updateMenuOrder(MangermoreActivity activity, List<PermissionMenuEntity> list){
        DialogHelperNew.showProgressDialog(activity, "排序中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().updateMenuOrder(list).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    Intent intent =new Intent();
                    activity.setResult(Activity.RESULT_OK,intent);
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
    public void restoreDefault(MangermoreActivity activity){
        DialogHelperNew.showProgressDialog(activity, "恢复排序中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().restoreDefault().enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    Intent intent =new Intent();
                    activity.setResult(Activity.RESULT_OK,intent);
                    activity.finish();
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
}
